package com.hhit.bigscreenbackmanage.service.impl.tgygl;

import com.alibaba.fastjson.JSONObject;
import com.hhit.bigscreenbackmanage.dao.tgygl.TTgyglLicenseProcessingMapper;
import com.hhit.bigscreenbackmanage.entity.tgygl.TTgyglLicenseProcessing;
import com.hhit.bigscreenbackmanage.service.tgygl.TTgyglLicenseProcessingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.hhit.bigscreenbackmanage.dao.tgygl.TTgyglLicenseProcessingMapper;
import com.hhit.bigscreenbackmanage.entity.tgygl.TTgyglConverterStation;
import com.hhit.bigscreenbackmanage.entity.tgygl.TTgyglLicenseProcessing;
import com.hhit.bigscreenbackmanage.service.tgygl.TTgyglLicenseProcessingService;
import org.seimicrawler.xpath.JXNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author  zhl
 * @Date 2021/7/22
 */
@Service
public class TTgyglLicenseProcessingServiceImpl implements TTgyglLicenseProcessingService {

    private Logger log = LoggerFactory.getLogger(TTgyglLicenseProcessingServiceImpl.class);

    @Autowired
    TTgyglLicenseProcessingMapper tTgyglLicenseProcessingMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int insert(TTgyglLicenseProcessing record) {
        return 0;
    }

    @Override
    public int insertSelective(TTgyglLicenseProcessing record) {
        return 0;
    }

    @Override
    public TTgyglLicenseProcessing selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(TTgyglLicenseProcessing record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(TTgyglLicenseProcessing record) {
        return 0;
    }

    @Override
    public List<Map<String, Object>> queryLicenseProcessingCount(JSONObject jsonObject) throws Exception {
        if (jsonObject.get("dataTime") == null || "".equals(jsonObject.get("dataTime"))) {
            throw new Exception("缺少必要参数: dataTime");
        }

        List<Map<String, Object>> result = new ArrayList<>();
        try {
            result = tTgyglLicenseProcessingMapper.queryLicenseProcessingCount(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new Exception("数据库内部错误");
        }
        return result;
    }

    @Override
    public void insertWord(JXNode jxNode, int id, int year, int month) throws Exception {

        //先删除对应的数据
        tTgyglLicenseProcessingMapper.deleteByNfAndFf(year,month);

        List<JXNode> trNodeList = jxNode.sel("//tr");
        //获取第一行的地址;
        List<JXNode> selAddress = trNodeList.get(0).sel("//td");
        String address1 = selAddress.get(1).selOne("//p//text()").toString();
        String address2 = selAddress.get(2).selOne("//p//text()").toString();
        String address3 = selAddress.get(3).selOne("//p//text()").toString();
        String address4 = selAddress.get(4).selOne("//p//text()").toString();

        for (JXNode item : trNodeList) {
            int index = trNodeList.indexOf(item);
            if (index != 0) {
                List<JXNode> tdNodeList = item.sel("//td");
                TTgyglLicenseProcessing tTgyglLicenseProcessing = new TTgyglLicenseProcessing();
                //年
                tTgyglLicenseProcessing.setYear(year);
                //月
                tTgyglLicenseProcessing.setMonth(month);
                //文件id
                tTgyglLicenseProcessing.setFileId(id);
                //事项
                tTgyglLicenseProcessing.setMatter(tdNodeList.get(0).selOne("//p//text()").toString());
                if (index == 7) {
                    System.out.println("11");
                    continue;
                }
                //要求完成时间
                tTgyglLicenseProcessing.setEndTime(
                        tdNodeList.get(5).selOne("//p//text()") == null ? "" : tdNodeList.get(5).selOne("//p//text()").toString());
                //评价
                tTgyglLicenseProcessing.setRemarks(
                        tdNodeList.get(6).selOne("//p//text()") == null ? "" : tdNodeList.get(6).selOne("//p//text()").toString());


                //地址
                tTgyglLicenseProcessing.setAddress(address1);
                //内容
                tTgyglLicenseProcessing.setContent(
                        tdNodeList.get(1).selOne("//p//text()") == null ? "" : tdNodeList.get(1).selOne("//p//text()").toString());
                //第一行数据
                tTgyglLicenseProcessingMapper.insert(tTgyglLicenseProcessing);

                //地址
                tTgyglLicenseProcessing.setAddress(address2);
                //内容
                tTgyglLicenseProcessing.setContent(
                        tdNodeList.get(2).selOne("//p//text()") == null ? "" : tdNodeList.get(2).selOne("//p//text()").toString());

                //第二行数据
                tTgyglLicenseProcessingMapper.insert(tTgyglLicenseProcessing);

                //地址
                tTgyglLicenseProcessing.setAddress(address3);
                //内容
                tTgyglLicenseProcessing.setContent(
                        tdNodeList.get(3).selOne("//p//text()") == null ? "" : tdNodeList.get(3).selOne("//p//text()").toString());
                //第三行数据
                tTgyglLicenseProcessingMapper.insert(tTgyglLicenseProcessing);

                //地址
                tTgyglLicenseProcessing.setAddress(address4);
                //内容
                tTgyglLicenseProcessing.setContent(
                        tdNodeList.get(4).selOne("//p//text()") == null ? "" : tdNodeList.get(4).selOne("//p//text()").toString());
                //第四行数据
                tTgyglLicenseProcessingMapper.insert(tTgyglLicenseProcessing);

            }
            System.out.println(item);
        }
    }

    @Override
    public List<TTgyglLicenseProcessing> selectListByFileId(Integer fileId) {
        List<TTgyglLicenseProcessing> resulList = null;
        TTgyglLicenseProcessing tTgyglLicenseProcessing = new TTgyglLicenseProcessing();
        tTgyglLicenseProcessing.setFileId(fileId);
        try {
            resulList = tTgyglLicenseProcessingMapper.selectByBean(tTgyglLicenseProcessing);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resulList;
    }
}
